import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import commonjsBuild from "@rollup/plugin-commonjs";
import commonjsServe from "vite-plugin-commonjs";
// import legacy from "@vitejs/plugin-legacy";
import path from "path";
// import AutoImport from "unplugin-auto-import/vite";
// import Components from "unplugin-vue-components/vite";
// import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

const myPlugin = {
  name: "vite-custom-plugin-test",
  apply: "build",
  closeBundle() {
    console.log("[自定义插件测试][closeBundle]");
  }
};

export default defineConfig({
  server: {
    host: "0.0.0.0",
    port: 3001,
    open: true,
    proxy: {
      "/api": {
        target: "http://132.232.91.155/shou_quan/",
        changeOrigin: true,
        configure: (proxy, options) => {
          console.log(proxy, options);
          // proxy.on("proxyReq", function (proxyReq, req, res) {
          //   // res.write();
          //   res.end(JSON.stringify({ a: 111 }));
          //   console.log("proxyReq", res.json);
          // });
        }
        // before() {
        //   console.log("===========");
        // }
      }
    }
  },
  base: "./",
  // assetsInclude: ["**/public/*"], //静态资源
  build: {
    outDir: "dist",
    assetsDir: "assets", //会被rollupOptions.output的配置覆盖
    input: {
      index: path.resolve(__dirname, "src/main.js"),
      name: "index"
    },
    cssCodeSplit: true,
    // lib: {
    //   entry: path.resolve(__dirname, "src/main.js"),
    //   name: "index",
    //   fileName: "index"
    //   // fileName: (format) => `visual-sdk/VisualAppLib.${format}.js`
    // },
    sourcemap: false, //是否打包源码映射文件
    assetsInlineLimit: 0, //此阈值的导入或引用资源将内联为 base64 编码
    reportCompressedSize: false,
    rollupOptions: {
      output: {
        chunkFileNames: "js/[name].[hash].js", //公共文件输出目录
        entryFileNames: "js/[name].[hash].js", //实体文件输出目录(业务主体文件)
        assetFileNames: (assetInfo) => {
          // console.debug("[静态资源编译输出][assetInfo]", assetInfo);
          //资源输出目录

          // var assets = [
          //   {
          //     test: /\.(png|jpe?g|gif|webp|avif|svg)(\?.*)?$/,
          //     assetDir: "img"
          //   },
          //   {
          //     test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i,
          //     assetDir: "fonts"
          //   },
          //   {
          //     test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
          //     assetDir: "media"
          //   }
          // ];

          let path = assetInfo.name || "";
          let pathArr = path.split("/") || [];
          let assetDir = pathArr[pathArr.length - 2];
          assetDir = assetDir ? `${assetDir}/` : "[ext]/";
          return `${assetDir}[name][extname]`; // 修改静态资源输出目录([ext]资源目录,[name]资源名,[extname]资源后缀)
        }
      }
    }
  },

  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src")
    },
    extensions: [".js", ".vue", ".json", ".ts"] // 使用路径别名时想要省略的后缀名，可以自己 增减
  },
  // css: {move
  //   preprocessorOptions: {
  //     scss: {
  //       additionalData: `@use "~/styles/element/index.scss" as *;`,
  //     },
  //   },
  // },
  define: { "process.env": {} }, //去除编译后process报错的问题
  plugins: [
    vue(),
    commonjsServe(),
    commonjsBuild()
    // legacy({
    //   targets: ["defaults", "not IE 11"]
    // })
    // AutoImport({
    //   resolvers: [ElementPlusResolver()]
    // }),
    // Components({
    //   resolvers: [ElementPlusResolver()]
    // }),
    // myPlugin
    // Components({
    //   // allow auto load markdown components under `./src/components/`
    //   extensions: ["vue", "md"],
    //   // allow auto import and register components used in markdown
    //   include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
    //   resolvers: [
    //     ElementPlusResolver({
    //       importStyle: "sass",
    //     }),
    //   ],
    //   dts: "src/components.d.ts",
    // }),
  ]
});
